/**
 * CoffeeMilk (C) 2010 Amen Hamdan
 * 
 * See the NOTICE file distributed with this work for additional 
 * information regarding copyright ownership.  This file is licenses
 * to you under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License.  You 
 * may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.    
 * 
 */
package ameen.rtm.lib.types;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class RtmList implements PropertyChangeListener {
	public static final String EXPANDED = "RtmList.isExpanded";
	public static final String TASK_ADDED = "RtmList.taskAdded";
	public static final String TASK_REMOVED = "RtmList.taskRemoved";
	
	private String fId, fName;
	private TasksVector fTasks;
	private boolean fIsExpanded = true;
    private final PropertyChangeSupport fPropertyChangeSupport = new PropertyChangeSupport(this);
	private boolean fIsSmart;

 
	public RtmList() {  }
	
	public RtmList(String id, String name, TasksVector tasks) {
		fId = id;
		fName = name;
		fTasks = tasks;
		fTasks.addPropertyChangeListener(this);
	}
	
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		this.fPropertyChangeSupport.addPropertyChangeListener(listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		this.fPropertyChangeSupport.removePropertyChangeListener(listener);
	}
	
	public String getId() {
		return fId;
	}

	public String getName() {
		return fName;
	}

	public boolean isExpanded() {
		return fIsExpanded;
	}

	public void setExpanded(boolean isExpanded) {
		boolean oldValue = fIsExpanded;
		fIsExpanded = isExpanded;
		fPropertyChangeSupport.firePropertyChange(EXPANDED, oldValue, fIsExpanded);
	}

	public void removeTask(RtmTask deleteTask) {
		fTasks.remove(deleteTask);
		fPropertyChangeSupport.firePropertyChange(TASK_REMOVED, deleteTask, null);
	}
	
	public void addTask(RtmTask newTask) {
		fTasks.add(0, newTask);
		fPropertyChangeSupport.firePropertyChange(TASK_ADDED, null, newTask);
	}
	
	public int getTaskCount() {
		return fTasks.size();
	}

	public void setId(String id) {
		this.fId = id;
	}

	public void setName(String name) {
		this.fName = name;
	}

	public TasksVector getTasks() {
		return fTasks;
	}

	public void setTasks(TasksVector fTasks) {
		this.fTasks = fTasks;
		fTasks.addPropertyChangeListener(this);
	}
	
	@Override
	public String toString() {
		return fName;
	}

	public void setIsSmart(boolean isSmart) {
		fIsSmart = isSmart;
	}
	
	public boolean isSmart() {
		return fIsSmart;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(evt.getPropertyName().equals(TasksVector.ELEMENT_REMOVED)) {
			fPropertyChangeSupport.firePropertyChange(TASK_REMOVED, evt.getOldValue(), null);
		}
	}
}
